package com.hymall.service.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hymall.bean.MarketStorage;
import com.hymall.bean.MarketStorageExample;
import com.hymall.bean.common.BasePageInfo;
import com.hymall.bean.common.CommonData;
import com.hymall.mapper.MarketStorageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author: 叶熠彬
 * @description:对象存储
 * @date: 2022/7/18 11:52
 */

@Service
public class StorageServiceImpl implements StorageService {

    @Autowired
    MarketStorageMapper storageMapper;

    /**
     * TODO 查询存储对象列表
     *
     * @author: 叶熠彬
     * @date: 2022/7/18 11:56
     * @param: [key, name, info]
     * @return: com.hymall.bean.common.CommonData
     */
    @Override
    public CommonData queryList(String key, String name, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit(),info.getSort()+" "+info.getOrder());
        MarketStorageExample example = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (key != null) {
            criteria.andKeyEqualTo(key);
        }
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        List<MarketStorage> marketStorages = storageMapper.selectByExample(example);
        PageInfo<MarketStorage> pageInfo = new PageInfo<>(marketStorages);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketStorage addImage(MultipartFile file) {
        //上传文件到本地
        String name = file.getOriginalFilename();
        String suffix = name.substring(name.indexOf("."));
        String uuid = UUID.randomUUID().toString();
        String key = uuid + suffix;
        long size = file.getSize();
        String type = file.getContentType();
        String url = "http://" + "localhost:8083" + "/image" + "/" + key;
        String filePath = "D:\\hymall\\image";

        File dest = new File(filePath + "/" + key);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        //写入数据库
        MarketStorage storage = new MarketStorage();
        storage.setAddTime(new Date());
        storage.setUpdateTime(new Date());
        storage.setDeleted(false);
        storage.setName(name);
        storage.setKey(key);
        storage.setSize(size);
        storage.setType(type);
        storage.setUrl(url);
        storageMapper.insertSelective(storage);
        return storage;
    }

    /**
     * 更新图片名字
     *
     * @author: 叶熠彬
     * @date: 2022/7/18 15:25
     * @param: [storage]
     * @return: com.hymall.bean.MarketStorage
     */
    @Override
    public MarketStorage updateImageName(MarketStorage storage) {
        storage.setUpdateTime(new Date());
        MarketStorageExample example = new MarketStorageExample();
        example.createCriteria().andIdEqualTo(storage.getId());
        storageMapper.updateByExampleSelective(storage, example);
        return storage;
    }

    /**
     * 逻辑删除图片
     *
     * @author: 叶熠彬
     * @date: 2022/7/18 15:31
     * @param: [storage]
     * @return: void
     */
    @Override
    public int deleteImage(MarketStorage storage) {
        storage.setUpdateTime(new Date());
        storage.setDeleted(true);
        MarketStorageExample example = new MarketStorageExample();
        example.createCriteria().andIdEqualTo(storage.getId());
        try {
            storageMapper.updateByExampleSelective(storage, example);
            return 200;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 201;
    }
}
